package com.ruoyi.project.cyq.item.archiving.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cyq.item.archiving.domain.*;
import com.ruoyi.project.cyq.item.archiving.service.IArchivingService;
import com.ruoyi.project.cyq.client.client.domain.Client;
import com.ruoyi.project.cyq.client.client.service.IClientService;
import com.ruoyi.project.cyq.item.item.domain.Item;
import com.ruoyi.project.cyq.item.item.domain.ItemVo;
import com.ruoyi.project.cyq.item.item.service.IItemService;
import com.ruoyi.project.cyq.order.domain.Order;
import com.ruoyi.project.cyq.order.domain.OrderProduct;
import com.ruoyi.project.cyq.order.service.IOrderService;
import com.ruoyi.project.cyq.personnelManagement.personnel.domain.Personnel;
import com.ruoyi.project.cyq.personnelManagement.personnel.service.IPersonnelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Strawberry
 * @date 2020-09-18 上午 11:53
 * @desc 快乐将永远依着草莓
 * @desc 项目资料归档管理的控制器
 */
@Controller
@RequestMapping("/cyq/item/archiving")
public class ArchivingController extends BaseController {

    private String prefix = "cyq/item/archiving";

    @Autowired
    private IArchivingService archivingService;
    /**
     * 客户的Service
     */
    @Autowired
    private IClientService clientService;
    /**
     * 项目的service
     */
    @Autowired
    private IItemService itemService;
    /**
     * 员工的service
     */
    @Autowired
    private IPersonnelService personnelService;
    /**
     * 订单的service
     */
    @Autowired
    private IOrderService orderService;


    /**
     * 管理主页的控制器
     */
    @RequiresPermissions("cyq:archiving:view")
    @GetMapping()
    public String itemItask() {
        return prefix + "/archiving";
    }

    /**
     * 项目的展示
     */
    @RequiresPermissions("cyq:archiving:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo showAllItem(ArchivingVo archiving) {
//        startPage();
        List<Archiving> archivingVo = archivingService.selectAllArchiving(archiving);
        return getDataTable(archivingVo);
    }

    /**
     * 项目审核窗口开启
     */
    @GetMapping("/edit/{projectId}")
    public String editArchiving(@PathVariable BigInteger projectId, ModelMap modelMap) {
//       获取审核的信息
        ArchivingVo archivingVo = archivingService.selectOneById(projectId);
        System.out.println(archivingVo);
        modelMap.addAttribute("review", archivingVo);

        BigInteger clientId = archivingVo.getClientId();
        System.out.println("clientId==>" + clientId);
        Client client = clientService.selectOneById(clientId);
        System.out.println("client ==> " + client);
        modelMap.addAttribute("client", client);
        return prefix + "/edit";
    }

    /**
     * 项目审核的保存
     */
    @RequiresPermissions("cyq:archiving:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult saveTheArchiving(Archiving archiving) {
        System.out.println("saveArchving ==> " + archiving);
        return toAjax(archivingService.updateArchivingResult(archiving));
    }

    /**
     * 新增一个审核项目
     */
    @GetMapping("/add")
    public String addProject(ModelMap modelMap, Personnel personnel) {
//        处理项目信息
        ArrayList<ItemWithStaff> itemWithStaffs = new ArrayList<>();
        List<Item> items = itemService.selectNotInReviewItem();

        for (Item i : items) {
                ItemWithStaff itemWithStaff = new ItemWithStaff();
                itemWithStaff.setItemId(i.getProjectId());
                itemWithStaff.setItemName(i.getProjectName());
                itemWithStaffs.add(itemWithStaff);
        }

//        处理员工信息
        ArrayList<StaffWithItem> staffWithItems = new ArrayList<>();
        List<Personnel> personnels = personnelService.selectPersonnelList(personnel);
        for (Personnel p : personnels) {
            StaffWithItem staffWithItem = new StaffWithItem();
            staffWithItem.setStaffId(p.getStaffId());
            staffWithItem.setStaffName(p.getStaffName());
            staffWithItems.add(staffWithItem);
        }

        System.out.println("===========ArchivingController--addProject--打印项目信息===========");
        itemWithStaffs.forEach(System.out::println);
        System.out.println("===========ArchivingController--addProject--打印员工信息===========");
        staffWithItems.forEach(System.out::println);

        modelMap.addAttribute("projectInfo", itemWithStaffs);
        modelMap.addAttribute("staffInfo", staffWithItems);
        return prefix + "/add";
    }

    /**
     * 新增项目审核的保存
     */
    @RequiresPermissions("cyq:archiving:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult saveAddProject(BigInteger itemId, BigInteger staffId) {
        return toAjax(archivingService.addNewArchiving(itemId, staffId));
    }

    /**
     * 查看项目具体信息
     */
    @RequiresPermissions("cyq:archiving:detail")
    @GetMapping("/detail/{reviewId}")
    public String detailArchiving(@PathVariable BigInteger reviewId, ModelMap modelMap) {
        ArchivingVo archivingVo = archivingService.selectOneById(reviewId);

//        通过项目id去查询项目信息
        ItemVo oneItem = itemService.selectOneById(archivingVo.getProjectId());
//        System.out.println("oneItem ==> " + oneItem);

//        通过员工的id去查询员工的名字,回写到itemVo中
        Personnel personnel = personnelService.selectPersonnelById(archivingVo.getStaffId());
        oneItem.setLeaderName(personnel.getStaffName());
//        通过客户id去查询客户
        Client client = clientService.selectOneById(archivingVo.getClientId());
//        通过订单Id去查询订单然后查询产品
        Order order = orderService.selectOrderByOrderNumber(Long.valueOf(archivingVo.getOrderNumber()));
        OrderProduct orderProduct = null;
        if (order != null) {
            orderProduct = archivingService.selectProjectById(order.getProductId());
        } else {
            orderProduct = new OrderProduct();
            orderProduct.setProductId(-1);
        }

        modelMap.addAttribute("oneItem", oneItem);
        modelMap.addAttribute("client", client);
        modelMap.addAttribute("orderProduct", orderProduct);

        return prefix + "/detail";
    }

}
